package com.itxx.treeSet;

import com.itxx.treeSet.domain.Student1;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import java.util.TreeSet;

public class TreeSetDemo2 {
    public static void main(String[] args) {
      /*  //treeSet集合  没有索引/元素唯一
        // 有序: 元素按照一定(某种规则)顺序排序     规则必须手动制定,没有制定会出现异常
             比较器排序:
              TreeSet<Student1> treeSet = new TreeSet<>(new Comparator<Student1>() {
            @Override
            public int compare(Student1 o1, Student1 o2) {
                int i = o1.getAge() - o2.getAge();
                if (i == 0) {
                    i = (int) (o1.getSorce() - o2.getSorce());
                }
                if (i == 0) {
                    Collator instance = Collator.getInstance(Locale.CHINESE);
                    i = instance.compare(o1.getName(), o2.getName());
                }
                return i;
            }
            });

                返回值为负数   存入的元素是较小值  存左边
                返回值为 0    重复元素  不存
                返回值为正数   存入的元素是较大值 存右边
          */


        TreeSet<Student1> treeSet = new TreeSet<>(new Comparator<Student1>() {
            @Override
            public int compare(Student1 o1, Student1 o2) {
                int i = o1.getAge() - o2.getAge();
                if (i == 0) {
                    i = (int) (o1.getSorce() - o2.getSorce());
                }
                if (i == 0) {
                    Collator instance = Collator.getInstance(Locale.CHINESE);
                    i = instance.compare(o1.getName(), o2.getName());
                }
                return i;
            }
        });
        treeSet.add(new Student1("张三", 27, 99.5));
        treeSet.add(new Student1("李四", 23, 60.0));
        treeSet.add(new Student1("王五", 27, 78.9));
        treeSet.add(new Student1("赵六", 16, 88.5));
        treeSet.add(new Student1("李元霸", 5, 99.0));
        System.out.println(treeSet);
        for (Student1 student1 : treeSet) {
            System.out.println(student1);
        }
    }
}
